home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / stk-3.002 / stk-3 / STk-3.1 / Tk / generic / tkListbox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-31  |  68.2 KB  |  2,287 lines

  1. /* 
  2.  * tkListbox.c --
  3.  *
  4.  *    This module implements listbox widgets for the Tk
  5.  *    toolkit.  A listbox displays a collection of strings,
  6.  *    one per line, and provides scrolling and selection.
  7.  *
  8.  * Copyright (c) 1990-1994 The Regents of the University of California.
  9.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  10.  *
  11.  * See the file "license.terms" for information on usage and redistribution
  12.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  13.  *
  14.  * SCCS: @(#) tkListbox.c 1.108 96/03/29 17:56:32
  15.  */
  16.  
  17. #include "tkPort.h"
  18. #include "default.h"
  19. #include "tkInt.h"
  20.  
  21. /*
  22.  * One record of the following type is kept for each element
  23.  * associated with a listbox widget:
  24.  */
  25.  
  26. typedef struct Element {
  27.     int textLength;        /* # non-NULL characters in text. */
  28.     int lBearing;        /* Distance from first character's
  29.                  * origin to left edge of character. */
  30.     int pixelWidth;        /* Total width of element in pixels (including
  31.                  * left bearing and right bearing). */
  32.     int selected;        /* 1 means this item is selected, 0 means
  33.                  * it isn't. */
  34.     struct Element *nextPtr;    /* Next in list of all elements of this
  35.                  * listbox, or NULL for last element. */
  36.     char text[4];        /* Characters of this element, NULL-
  37.                  * terminated.  The actual space allocated
  38.                  * here will be as large as needed (> 4,
  39.                  * most likely).  Must be the last field
  40.                  * of the record. */
  41. } Element;
  42.  
  43. #define ElementSize(stringLength) \
  44.     ((unsigned) (sizeof(Element) - 3 + stringLength))
  45.  
  46. /*
  47.  * A data structure of the following type is kept for each listbox
  48.  * widget managed by this file:
  49.  */
  50.  
  51. typedef struct {
  52.     Tk_Window tkwin;        /* Window that embodies the listbox.  NULL
  53.                  * means that the window has been destroyed
  54.                  * but the data structures haven't yet been
  55.                  * cleaned up.*/
  56.     Display *display;        /* Display containing widget.  Used, among
  57.                  * other things, so that resources can be
  58.                  * freed even after tkwin has gone away. */
  59.     Tcl_Interp *interp;        /* Interpreter associated with listbox. */
  60.     Tcl_Command widgetCmd;    /* Token for listbox's widget command. */
  61.     int numElements;        /* Total number of elements in this listbox. */
  62.     Element *firstPtr;        /* First in list of elements (NULL if no
  63.                  * elements). */
  64.     Element *lastPtr;        /* Last in list of elements (NULL if no
  65.                  * elements). */
  66.  
  67.     /*
  68.      * Information used when displaying widget:
  69.      */
  70.  
  71.     Tk_3DBorder normalBorder;    /* Used for drawing border around whole
  72.                  * window, plus used for background. */
  73.     int borderWidth;        /* Width of 3-D border around window. */
  74.     int relief;            /* 3-D effect: TK_RELIEF_RAISED, etc. */
  75.     int highlightWidth;        /* Width in pixels of highlight to draw
  76.                  * around widget when it has the focus.
  77.                  * <= 0 means don't draw a highlight. */
  78.     XColor *highlightBgColorPtr;
  79.                 /* Color for drawing traversal highlight
  80.                  * area when highlight is off. */
  81.     XColor *highlightColorPtr;    /* Color for drawing traversal highlight. */
  82.     int inset;            /* Total width of all borders, including
  83.                  * traversal highlight and 3-D border.
  84.                  * Indicates how much interior stuff must
  85.                  * be offset from outside edges to leave
  86.                  * room for borders. */
  87.     XFontStruct *fontPtr;    /* Information about text font, or NULL. */
  88.     XColor *fgColorPtr;        /* Text color in normal mode. */
  89.     GC textGC;            /* For drawing normal text. */
  90.     Tk_3DBorder selBorder;    /* Borders and backgrounds for selected
  91.                  * elements. */
  92.     int selBorderWidth;        /* Width of border around selection. */
  93.     XColor *selFgColorPtr;    /* Foreground color for selected elements. */
  94.     GC selTextGC;        /* For drawing selected text. */
  95.     int width;            /* Desired width of window, in characters. */
  96.     int height;            /* Desired height of window, in lines. */
  97.     int lineHeight;        /* Number of pixels allocated for each line
  98.                  * in display. */
  99.     int topIndex;        /* Index of top-most element visible in
  100.                  * window. */
  101.     int fullLines;        /* Number of lines that fit are completely
  102.                  * visible in window.  There may be one
  103.                  * additional line at the bottom that is
  104.                  * partially visible. */
  105.     int partialLine;        /* 0 means that the window holds exactly
  106.                  * fullLines lines.  1 means that there is
  107.                  * one additional line that is partially
  108.                  * visble. */
  109.     int setGrid;        /* Non-zero means pass gridding information
  110.                  * to window manager. */
  111.  
  112.     /*
  113.      * Information to support horizontal scrolling:
  114.      */
  115.  
  116.     int maxWidth;        /* Width (in pixels) of widest string in
  117.                  * listbox. */
  118.     int xScrollUnit;        /* Number of pixels in one "unit" for
  119.                  * horizontal scrolling (window scrolls
  120.                  * horizontally in increments of this size).
  121.                  * This is an average character size. */
  122.     int xOffset;        /* The left edge of each string in the
  123.                  * listbox is offset to the left by this
  124.                  * many pixels (0 means no offset, positive
  125.                  * means there is an offset). */
  126.  
  127.     /*
  128.      * Information about what's selected or active, if any.
  129.      */
  130.  
  131.     Tk_Uid selectMode;        /* Selection style: single, browse, multiple,
  132.                  * or extended.  This value isn't used in C
  133.                  * code, but the Tcl bindings use it. */
  134.     int numSelected;        /* Number of elements currently selected. */
  135.     int selectAnchor;        /* Fixed end of selection (i.e. element
  136.                  * at which selection was started.) */
  137.     int exportSelection;    /* Non-zero means tie internal listbox
  138.                  * to X selection. */
  139.     int active;            /* Index of "active" element (the one that
  140.                  * has been selected by keyboard traversal).
  141.                  * -1 means none. */
  142.  
  143.     /*
  144.      * Information for scanning:
  145.      */
  146.  
  147.     int scanMarkX;        /* X-position at which scan started (e.g.
  148.                  * button was pressed here). */
  149.     int scanMarkY;        /* Y-position at which scan started (e.g.
  150.                  * button was pressed here). */
  151.     int scanMarkXOffset;    /* Value of "xOffset" field when scan
  152.                  * started. */
  153.     int scanMarkYIndex;        /* Index of line that was at top of window
  154.                  * when scan started. */
  155.  
  156.     /*
  157.      * Miscellaneous information:
  158.      */
  159.  
  160.     Tk_Cursor cursor;        /* Current cursor for window, or None. */
  161.     char *takeFocus;        /* Value of -takefocus option;  not used in
  162.                  * the C code, but used by keyboard traversal
  163.                  * scripts.  Malloc'ed, but may be NULL. */
  164.     char *yScrollCmd;        /* Command prefix for communicating with
  165.                  * vertical scrollbar.  NULL means no command
  166.                  * to issue.  Malloc'ed. */
  167.     char *xScrollCmd;        /* Command prefix for communicating with
  168.                  * horizontal scrollbar.  NULL means no command
  169.                  * to issue.  Malloc'ed. */
  170.     int flags;            /* Various flag bits:  see below for
  171.                  * definitions. */
  172. } Listbox;
  173.  
  174. /*
  175.  * Flag bits for listboxes:
  176.  *
  177.  * REDRAW_PENDING:        Non-zero means a DoWhenIdle handler
  178.  *                has already been queued to redraw
  179.  *                this window.
  180.  * UPDATE_V_SCROLLBAR:        Non-zero means vertical scrollbar needs
  181.  *                to be updated.
  182.  * UPDATE_H_SCROLLBAR:        Non-zero means horizontal scrollbar needs
  183.  *                to be updated.
  184.  * GOT_FOCUS:            Non-zero means this widget currently
  185.  *                has the input focus.
  186.  */
  187.  
  188. #define REDRAW_PENDING        1
  189. #define UPDATE_V_SCROLLBAR    2
  190. #define UPDATE_H_SCROLLBAR    4
  191. #define GOT_FOCUS        8
  192.  
  193. /*
  194.  * Information used for argv parsing:
  195.  */
  196.  
  197. static Tk_ConfigSpec configSpecs[] = {
  198.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  199.     DEF_LISTBOX_BG_COLOR, Tk_Offset(Listbox, normalBorder),
  200.     TK_CONFIG_COLOR_ONLY},
  201.     {TK_CONFIG_BORDER, "-background", "background", "Background",
  202.     DEF_LISTBOX_BG_MONO, Tk_Offset(Listbox, normalBorder),
  203.     TK_CONFIG_MONO_ONLY},
  204.     {TK_CONFIG_SYNONYM, "-bd", "borderWidth", (char *) NULL,
  205.     (char *) NULL, 0, 0},
  206.     {TK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
  207.     (char *) NULL, 0, 0},
  208.     {TK_CONFIG_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
  209.     DEF_LISTBOX_BORDER_WIDTH, Tk_Offset(Listbox, borderWidth), 0},
  210.     {TK_CONFIG_ACTIVE_CURSOR, "-cursor", "cursor", "Cursor",
  211.     DEF_LISTBOX_CURSOR, Tk_Offset(Listbox, cursor), TK_CONFIG_NULL_OK},
  212.     {TK_CONFIG_BOOLEAN, "-exportselection", "exportSelection",
  213.     "ExportSelection", DEF_LISTBOX_EXPORT_SELECTION,
  214.     Tk_Offset(Listbox, exportSelection), 0},
  215.     {TK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
  216.     (char *) NULL, 0, 0},
  217.     {TK_CONFIG_FONT, "-font", "font", "Font",
  218.     DEF_LISTBOX_FONT, Tk_Offset(Listbox, fontPtr), 0},
  219.     {TK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
  220.     DEF_LISTBOX_FG, Tk_Offset(Listbox, fgColorPtr), 0},
  221.     {TK_CONFIG_INT, "-height", "height", "Height",
  222.     DEF_LISTBOX_HEIGHT, Tk_Offset(Listbox, height), 0},
  223.     {TK_CONFIG_COLOR, "-highlightbackground", "highlightBackground",
  224.     "HighlightBackground", DEF_LISTBOX_HIGHLIGHT_BG,
  225.     Tk_Offset(Listbox, highlightBgColorPtr), 0},
  226.     {TK_CONFIG_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
  227.     DEF_LISTBOX_HIGHLIGHT, Tk_Offset(Listbox, highlightColorPtr), 0},
  228.     {TK_CONFIG_PIXELS, "-highlightthickness", "highlightThickness",
  229.     "HighlightThickness",
  230.     DEF_LISTBOX_HIGHLIGHT_WIDTH, Tk_Offset(Listbox, highlightWidth), 0},
  231.     {TK_CONFIG_RELIEF, "-relief", "relief", "Relief",
  232.     DEF_LISTBOX_RELIEF, Tk_Offset(Listbox, relief), 0},
  233.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  234.     DEF_LISTBOX_SELECT_COLOR, Tk_Offset(Listbox, selBorder),
  235.     TK_CONFIG_COLOR_ONLY},
  236.     {TK_CONFIG_BORDER, "-selectbackground", "selectBackground", "Foreground",
  237.     DEF_LISTBOX_SELECT_MONO, Tk_Offset(Listbox, selBorder),
  238.     TK_CONFIG_MONO_ONLY},
  239.     {TK_CONFIG_PIXELS, "-selectborderwidth", "selectBorderWidth", "BorderWidth",
  240.     DEF_LISTBOX_SELECT_BD, Tk_Offset(Listbox, selBorderWidth), 0},
  241.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  242.     DEF_LISTBOX_SELECT_FG_COLOR, Tk_Offset(Listbox, selFgColorPtr),
  243.     TK_CONFIG_COLOR_ONLY},
  244.     {TK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
  245.     DEF_LISTBOX_SELECT_FG_MONO, Tk_Offset(Listbox, selFgColorPtr),
  246.     TK_CONFIG_MONO_ONLY},
  247.     {TK_CONFIG_UID, "-selectmode", "selectMode", "SelectMode",
  248.     DEF_LISTBOX_SELECT_MODE, Tk_Offset(Listbox, selectMode), 0},
  249.     {TK_CONFIG_BOOLEAN, "-setgrid", "setGrid", "SetGrid",
  250.     DEF_LISTBOX_SET_GRID, Tk_Offset(Listbox, setGrid), 0},
  251. #ifdef STk_CODE
  252.     {TK_CONFIG_CLOSURE, "-takefocus", "takeFocus", "TakeFocus",
  253. #else
  254.     {TK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
  255. #endif
  256.     DEF_LISTBOX_TAKE_FOCUS, Tk_Offset(Listbox, takeFocus),
  257.     TK_CONFIG_NULL_OK},
  258.     {TK_CONFIG_INT, "-width", "width", "Width",
  259.     DEF_LISTBOX_WIDTH, Tk_Offset(Listbox, width), 0},
  260. #ifdef STk_CODE
  261.     {TK_CONFIG_CLOSURE, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  262. #else
  263.     {TK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
  264. #endif
  265.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, xScrollCmd),
  266.     TK_CONFIG_NULL_OK},
  267. #ifdef STk_CODE
  268.     {TK_CONFIG_CLOSURE, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
  269. #else
  270.     {TK_CONFIG_STRING, "-yscrollcommand", "yScrollCommand", "ScrollCommand",
  271. #endif
  272.     DEF_LISTBOX_SCROLL_COMMAND, Tk_Offset(Listbox, yScrollCmd),
  273.     TK_CONFIG_NULL_OK},
  274.     {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
  275.     (char *) NULL, 0, 0}
  276. };
  277.  
  278. /*
  279.  * Forward declarations for procedures defined later in this file:
  280.  */
  281.  
  282. static void        ChangeListboxOffset _ANSI_ARGS_((Listbox *listPtr,
  283.                 int offset));
  284. static void        ChangeListboxView _ANSI_ARGS_((Listbox *listPtr,
  285.                 int index));
  286. static int        ConfigureListbox _ANSI_ARGS_((Tcl_Interp *interp,
  287.                 Listbox *listPtr, int argc, char **argv,
  288.                 int flags));
  289. static void        DeleteEls _ANSI_ARGS_((Listbox *listPtr, int first,
  290.                 int last));
  291. static void        DestroyListbox _ANSI_ARGS_((char *memPtr));
  292. static void        DisplayListbox _ANSI_ARGS_((ClientData clientData));
  293. static int        GetListboxIndex _ANSI_ARGS_((Tcl_Interp *interp,
  294.                 Listbox *listPtr, char *string, int numElsOK,
  295.                 int *indexPtr));
  296. static void        InsertEls _ANSI_ARGS_((Listbox *listPtr, int index,
  297.                 int argc, char **argv));
  298. static void        ListboxCmdDeletedProc _ANSI_ARGS_((
  299.                 ClientData clientData));
  300. static void        ListboxComputeGeometry _ANSI_ARGS_((Listbox *listPtr,
  301.                 int fontChanged, int maxIsStale, int updateGrid));
  302. static void        ListboxEventProc _ANSI_ARGS_((ClientData clientData,
  303.                 XEvent *eventPtr));
  304. static int        ListboxFetchSelection _ANSI_ARGS_((
  305.                 ClientData clientData, int offset, char *buffer,
  306.                 int maxBytes));
  307. static void        ListboxLostSelection _ANSI_ARGS_((
  308.                 ClientData clientData));
  309. static void        ListboxRedrawRange _ANSI_ARGS_((Listbox *listPtr,
  310.                 int first, int last));
  311. static void        ListboxScanTo _ANSI_ARGS_((Listbox *listPtr,
  312.                 int x, int y));
  313. static void        ListboxSelect _ANSI_ARGS_((Listbox *listPtr,
  314.                 int first, int last, int select));
  315. static void        ListboxUpdateHScrollbar _ANSI_ARGS_((Listbox *listPtr));
  316. static void        ListboxUpdateVScrollbar _ANSI_ARGS_((Listbox *listPtr));
  317. static int        ListboxWidgetCmd _ANSI_ARGS_((ClientData clientData,
  318.                 Tcl_Interp *interp, int argc, char **argv));
  319. static int        NearestListboxElement _ANSI_ARGS_((Listbox *listPtr,
  320.                 int y));
  321.  
  322. /*
  323.  *--------------------------------------------------------------
  324.  *
  325.  * Tk_ListboxCmd --
  326.  *
  327.  *    This procedure is invoked to process the "listbox" Tcl
  328.  *    command.  See the user documentation for details on what
  329.  *    it does.
  330.  *
  331.  * Results:
  332.  *    A standard Tcl result.
  333.  *
  334.  * Side effects:
  335.  *    See the user documentation.
  336.  *
  337.  *--------------------------------------------------------------
  338.  */
  339.  
  340. int
  341. Tk_ListboxCmd(clientData, interp, argc, argv)
  342.     ClientData clientData;    /* Main window associated with
  343.                  * interpreter. */
  344.     Tcl_Interp *interp;        /* Current interpreter. */
  345.     int argc;            /* Number of arguments. */
  346.     char **argv;        /* Argument strings. */
  347. {
  348.     register Listbox *listPtr;
  349.     Tk_Window new;
  350.     Tk_Window tkwin = (Tk_Window) clientData;
  351.  
  352.     if (argc < 2) {
  353.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  354.         argv[0], " pathName ?options?\"", (char *) NULL);
  355.     return TCL_ERROR;
  356.     }
  357.  
  358.     new = Tk_CreateWindowFromPath(interp, tkwin, argv[1], (char *) NULL);
  359.     if (new == NULL) {
  360.     return TCL_ERROR;
  361.     }
  362.  
  363.     /*
  364.      * Initialize the fields of the structure that won't be initialized
  365.      * by ConfigureListbox, or that ConfigureListbox requires to be
  366.      * initialized already (e.g. resource pointers).
  367.      */
  368.  
  369.     listPtr = (Listbox *) ckalloc(sizeof(Listbox));
  370.     listPtr->tkwin = new;
  371.     listPtr->display = Tk_Display(new);
  372.     listPtr->interp = interp;
  373.     listPtr->widgetCmd = Tcl_CreateCommand(interp,
  374.         Tk_PathName(listPtr->tkwin), ListboxWidgetCmd,
  375.         (ClientData) listPtr, ListboxCmdDeletedProc);
  376.     listPtr->numElements = 0;
  377.     listPtr->firstPtr = NULL;
  378.     listPtr->lastPtr = NULL;
  379.     listPtr->normalBorder = NULL;
  380.     listPtr->borderWidth = 0;
  381.     listPtr->relief = TK_RELIEF_RAISED;
  382.     listPtr->highlightWidth = 0;
  383.     listPtr->highlightBgColorPtr = NULL;
  384.     listPtr->highlightColorPtr = NULL;
  385.     listPtr->inset = 0;
  386.     listPtr->fontPtr = NULL;
  387.     listPtr->fgColorPtr = NULL;
  388.     listPtr->textGC = None;
  389.     listPtr->selBorder = NULL;
  390.     listPtr->selBorderWidth = 0;
  391.     listPtr->selFgColorPtr = None;
  392.     listPtr->selTextGC = None;
  393.     listPtr->width = 0;
  394.     listPtr->height = 0;
  395.     listPtr->lineHeight = 0;
  396.     listPtr->topIndex = 0;
  397.     listPtr->fullLines = 1;
  398.     listPtr->partialLine = 0;
  399.     listPtr->setGrid = 0;
  400.     listPtr->maxWidth = 0;
  401.     listPtr->xScrollUnit = 0;
  402.     listPtr->xOffset = 0;
  403.     listPtr->selectMode = NULL;
  404.     listPtr->numSelected = 0;
  405.     listPtr->selectAnchor = 0;
  406.     listPtr->exportSelection = 1;
  407.     listPtr->active = 0;
  408.     listPtr->scanMarkX = 0;
  409.     listPtr->scanMarkY = 0;
  410.     listPtr->scanMarkXOffset = 0;
  411.     listPtr->scanMarkYIndex = 0;
  412.     listPtr->cursor = None;
  413.     listPtr->takeFocus = NULL;
  414.     listPtr->xScrollCmd = NULL;
  415.     listPtr->yScrollCmd = NULL;
  416.     listPtr->flags = 0;
  417.  
  418.     Tk_SetClass(listPtr->tkwin, "Listbox");
  419.     Tk_CreateEventHandler(listPtr->tkwin,
  420.         ExposureMask|StructureNotifyMask|FocusChangeMask,
  421.         ListboxEventProc, (ClientData) listPtr);
  422.     Tk_CreateSelHandler(listPtr->tkwin, XA_PRIMARY, XA_STRING,
  423.         ListboxFetchSelection, (ClientData) listPtr, XA_STRING);
  424.     if (ConfigureListbox(interp, listPtr, argc-2, argv+2, 0) != TCL_OK) {
  425.     goto error;
  426.     }
  427.  
  428. #ifdef STk_CODE
  429.     STk_sharp_dot_result(interp, Tk_PathName(listPtr->tkwin));
  430. #else
  431.     interp->result = Tk_PathName(listPtr->tkwin);
  432. #endif
  433.     return TCL_OK;
  434.  
  435.     error:
  436.     Tk_DestroyWindow(listPtr->tkwin);
  437.     return TCL_ERROR;
  438. }
  439.  
  440. /*
  441.  *--------------------------------------------------------------
  442.  *
  443.  * ListboxWidgetCmd --
  444.  *
  445.  *    This procedure is invoked to process the Tcl command
  446.  *    that corresponds to a widget managed by this module.
  447.  *    See the user documentation for details on what it does.
  448.  *
  449.  * Results:
  450.  *    A standard Tcl result.
  451.  *
  452.  * Side effects:
  453.  *    See the user documentation.
  454.  *
  455.  *--------------------------------------------------------------
  456.  */
  457.  
  458. static int
  459. ListboxWidgetCmd(clientData, interp, argc, argv)
  460.     ClientData clientData;        /* Information about listbox widget. */
  461.     Tcl_Interp *interp;            /* Current interpreter. */
  462.     int argc;                /* Number of arguments. */
  463.     char **argv;            /* Argument strings. */
  464. {
  465.     register Listbox *listPtr = (Listbox *) clientData;
  466.     int result = TCL_OK;
  467.     size_t length;
  468.     int c;
  469.  
  470.     if (argc < 2) {
  471.     Tcl_AppendResult(interp, "wrong # args: should be \"",
  472.         argv[0], " option ?arg arg ...?\"", (char *) NULL);
  473.     return TCL_ERROR;
  474.     }
  475.     Tcl_Preserve((ClientData) listPtr);
  476.     c = argv[1][0];
  477.     length = strlen(argv[1]);
  478.     if ((c == 'a') && (strncmp(argv[1], "activate", length) == 0)) {
  479.     int index;
  480.  
  481.     if (argc != 3) {
  482.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  483.             argv[0], " activate index\"",
  484.             (char *) NULL);
  485.         goto error;
  486.     }
  487.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  488.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  489.         != TCL_OK) {
  490.         goto error;
  491.     }
  492.     listPtr->active = index;
  493.     ListboxRedrawRange(listPtr, listPtr->active, listPtr->active);
  494.     } else if ((c == 'b') && (strncmp(argv[1], "bbox", length) == 0)) {
  495.     int index, x, y, i;
  496.     Element *elPtr;
  497.  
  498.     if (argc != 3) {
  499.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  500.             argv[0], " bbox index\"", (char *) NULL);
  501.         goto error;
  502.     }
  503.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  504.         goto error;
  505.     }
  506.     for (i = 0, elPtr = listPtr->firstPtr; i < index;
  507.         i++, elPtr = elPtr->nextPtr) {
  508.         /* Empty loop body. */
  509.     }
  510.     if ((index >= listPtr->topIndex) && (index < listPtr->numElements)
  511.             && (index < (listPtr->topIndex + listPtr->fullLines
  512.             + listPtr->partialLine))) {
  513.         x = listPtr->inset - listPtr->xOffset;
  514.         y = ((index - listPtr->topIndex)*listPtr->lineHeight)
  515.             + listPtr->inset + listPtr->selBorderWidth;
  516.         sprintf(interp->result, "%d %d %d %d", x, y, elPtr->pixelWidth,
  517.             listPtr->fontPtr->ascent + listPtr->fontPtr->descent);
  518.     }
  519.     } else if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
  520.         && (length >= 2)) {
  521.     if (argc != 3) {
  522.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  523.             argv[0], " cget option\"",
  524.             (char *) NULL);
  525.         goto error;
  526.     }
  527.     result = Tk_ConfigureValue(interp, listPtr->tkwin, configSpecs,
  528.         (char *) listPtr, argv[2], 0);
  529.     } else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
  530.         && (length >= 2)) {
  531.     if (argc == 2) {
  532.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  533.             (char *) listPtr, (char *) NULL, 0);
  534.     } else if (argc == 3) {
  535.         result = Tk_ConfigureInfo(interp, listPtr->tkwin, configSpecs,
  536.             (char *) listPtr, argv[2], 0);
  537.     } else {
  538.         result = ConfigureListbox(interp, listPtr, argc-2, argv+2,
  539.             TK_CONFIG_ARGV_ONLY);
  540.     }
  541.     } else if ((c == 'c') && (strncmp(argv[1], "curselection", length) == 0)
  542.         && (length >= 2)) {
  543.     int i, count;
  544.     char index[20];
  545.     Element *elPtr;
  546.  
  547.     if (argc != 2) {
  548.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  549.             argv[0], " curselection\"",
  550.             (char *) NULL);
  551.         goto error;
  552.     }
  553. #ifdef STk_CODE
  554.     Tcl_AppendResult(interp, "(", NULL);
  555. #endif
  556.     count = 0;
  557.     for (i = 0, elPtr = listPtr->firstPtr; elPtr != NULL;
  558.         i++, elPtr = elPtr->nextPtr) {
  559.         if (elPtr->selected) {
  560.         sprintf(index, "%d", i);
  561.         Tcl_AppendElement(interp, index);
  562.         count++;
  563.         }
  564.     }
  565. #ifdef STk_CODE
  566.     Tcl_AppendResult(interp, ")", NULL);
  567. #endif
  568.     if (count != listPtr->numSelected) {
  569.         panic("ListboxWidgetCmd: selection count incorrect");
  570.     }
  571.     } else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
  572.     int first, last;
  573.  
  574.     if ((argc < 3) || (argc > 4)) {
  575.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  576.             argv[0], " delete firstIndex ?lastIndex?\"",
  577.             (char *) NULL);
  578.         goto error;
  579.     }
  580.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  581.         goto error;
  582.     }
  583.     if (argc == 3) {
  584.         last = first;
  585.     } else {
  586.         if (GetListboxIndex(interp, listPtr, argv[3], 0, &last) != TCL_OK) {
  587.         goto error;
  588.         }
  589.     }
  590.     DeleteEls(listPtr, first, last);
  591.     } else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
  592.     int first, last, i;
  593.     Element *elPtr;
  594.  
  595.     if ((argc != 3) && (argc != 4)) {
  596.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  597.             argv[0], " get first ?last?\"", (char *) NULL);
  598.         goto error;
  599.     }
  600.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &first) != TCL_OK) {
  601.         goto error;
  602.     }
  603.     if ((argc == 4) && (GetListboxIndex(interp, listPtr, argv[3],
  604.         0, &last) != TCL_OK)) {
  605.         goto error;
  606.     }
  607.     for (elPtr = listPtr->firstPtr, i = 0; i < first;
  608.         i++, elPtr = elPtr->nextPtr) {
  609.         /* Empty loop body. */
  610.     }
  611.     if (elPtr != NULL) {
  612. #ifdef STk_CODE
  613.         if (argc == 3) {
  614.         STk_stringify_result(interp, elPtr->text);
  615.         } else {
  616.             Tcl_AppendResult(interp, "(", NULL);
  617.         for (  ; i <= last; i++, elPtr = elPtr->nextPtr) {
  618.           Tcl_AppendResult(interp, " ", 
  619.                    STk_stringify(elPtr->text, 0), NULL);
  620.         }
  621.         Tcl_AppendResult(interp, ")", NULL);
  622.         }
  623. #else
  624.         if (argc == 3) {
  625.         interp->result = elPtr->text;
  626.         } else {
  627.         for (  ; i <= last; i++, elPtr = elPtr->nextPtr) {
  628.             Tcl_AppendElement(interp, elPtr->text);
  629.         }
  630.         }
  631. #endif
  632.     }
  633.     } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
  634.         && (length >= 3)) {
  635.     int index;
  636.  
  637.     if (argc != 3) {
  638.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  639.             argv[0], " index index\"",
  640.             (char *) NULL);
  641.         goto error;
  642.     }
  643.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  644.         != TCL_OK) {
  645.         goto error;
  646.     }
  647.     sprintf(interp->result, "%d", index);
  648.     } else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
  649.         && (length >= 3)) {
  650.     int index;
  651.  
  652.     if (argc < 3) {
  653.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  654.             argv[0], " insert index ?element element ...?\"",
  655.             (char *) NULL);
  656.         goto error;
  657.     }
  658.     if (GetListboxIndex(interp, listPtr, argv[2], 1, &index)
  659.         != TCL_OK) {
  660.         goto error;
  661.     }
  662.     InsertEls(listPtr, index, argc-3, argv+3);
  663.     } else if ((c == 'n') && (strncmp(argv[1], "nearest", length) == 0)) {
  664.     int index, y;
  665.  
  666.     if (argc != 3) {
  667.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  668.             argv[0], " nearest y\"", (char *) NULL);
  669.         goto error;
  670.     }
  671.     if (Tcl_GetInt(interp, argv[2], &y) != TCL_OK) {
  672.         goto error;
  673.     }
  674.     index = NearestListboxElement(listPtr, y);
  675.     sprintf(interp->result, "%d", index);
  676.     } else if ((c == 's') && (length >= 2)
  677.         && (strncmp(argv[1], "scan", length) == 0)) {
  678.     int x, y;
  679.  
  680.     if (argc != 5) {
  681.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  682.             argv[0], " scan mark|dragto x y\"", (char *) NULL);
  683.         goto error;
  684.     }
  685.     if ((Tcl_GetInt(interp, argv[3], &x) != TCL_OK)
  686.         || (Tcl_GetInt(interp, argv[4], &y) != TCL_OK)) {
  687.         goto error;
  688.     }
  689.     if ((argv[2][0] == 'm')
  690.         && (strncmp(argv[2], "mark", strlen(argv[2])) == 0)) {
  691.         listPtr->scanMarkX = x;
  692.         listPtr->scanMarkY = y;
  693.         listPtr->scanMarkXOffset = listPtr->xOffset;
  694.         listPtr->scanMarkYIndex = listPtr->topIndex;
  695.     } else if ((argv[2][0] == 'd')
  696.         && (strncmp(argv[2], "dragto", strlen(argv[2])) == 0)) {
  697.         ListboxScanTo(listPtr, x, y);
  698.     } else {
  699.         Tcl_AppendResult(interp, "bad scan option \"", argv[2],
  700.             "\": must be mark or dragto", (char *) NULL);
  701.         goto error;
  702.     }
  703.     } else if ((c == 's') && (strncmp(argv[1], "see", length) == 0)
  704.         && (length >= 3)) {
  705.     int index, diff;
  706.     if (argc != 3) {
  707.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  708.             argv[0], " see index\"",
  709.             (char *) NULL);
  710.         goto error;
  711.     }
  712.     if (GetListboxIndex(interp, listPtr, argv[2], 0, &index) != TCL_OK) {
  713.         goto error;
  714.     }
  715.     diff = listPtr->topIndex-index;
  716.     if (diff > 0) {
  717.         if (diff <= (listPtr->fullLines/3)) {
  718.         ChangeListboxView(listPtr, index);
  719.         } else {
  720.         ChangeListboxView(listPtr, index - (listPtr->fullLines-1)/2);
  721.         }
  722.     } else {
  723.         diff = index - (listPtr->topIndex + listPtr->fullLines - 1);
  724.         if (diff > 0) {
  725.         if (diff <= (listPtr->fullLines/3)) {
  726.             ChangeListboxView(listPtr, listPtr->topIndex + diff);
  727.         } else {
  728.             ChangeListboxView(listPtr,
  729.                 index - (listPtr->fullLines-1)/2);
  730.         }
  731.         }
  732.     }
  733.     } else if ((c == 's') && (length >= 3)
  734.         && (strncmp(argv[1], "selection", length) == 0)) {
  735.     int first, last;
  736.  
  737.     if ((argc != 4) && (argc != 5)) {
  738.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  739.             argv[0], " selection option index ?index?\"",
  740.             (char *) NULL);
  741.         goto error;
  742.     }
  743.     if (GetListboxIndex(interp, listPtr, argv[3], 0, &first) != TCL_OK) {
  744.         goto error;
  745.     }
  746.     if (argc == 5) {
  747.         if (GetListboxIndex(interp, listPtr, argv[4], 0, &last) != TCL_OK) {
  748.         goto error;
  749.         }
  750.     } else {
  751.         last = first;
  752.     }
  753.     length = strlen(argv[2]);
  754.     c = argv[2][0];
  755.     if ((c == 'a') && (strncmp(argv[2], "anchor", length) == 0)) {
  756.         if (argc != 4) {
  757.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  758.             argv[0], " selection anchor index\"", (char *) NULL);
  759.         goto error;
  760.         }
  761.         listPtr->selectAnchor = first;
  762.     } else if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
  763.         ListboxSelect(listPtr, first, last, 0);
  764.     } else if ((c == 'i') && (strncmp(argv[2], "includes", length) == 0)) {
  765.         int i;
  766.         Element *elPtr;
  767.     
  768.         if (argc != 4) {
  769.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  770.             argv[0], " selection includes index\"", (char *) NULL);
  771.         goto error;
  772.         }
  773.         for (elPtr = listPtr->firstPtr, i = 0; i < first;
  774.             i++, elPtr = elPtr->nextPtr) {
  775.         /* Empty loop body. */
  776.         }
  777. #ifdef STk_CODE
  778.         interp->result = ((elPtr != NULL) && (elPtr->selected)) ? "#t" : "#f";
  779. #else
  780.         if ((elPtr != NULL) && (elPtr->selected)) {
  781.         interp->result = "1";
  782.         } else {
  783.         interp->result = "0";
  784.         }
  785. #endif
  786.     } else if ((c == 's') && (strncmp(argv[2], "set", length) == 0)) {
  787.         ListboxSelect(listPtr, first, last, 1);
  788.     } else {
  789.         Tcl_AppendResult(interp, "bad selection option \"", argv[2],
  790.             "\": must be anchor, clear, includes, or set",
  791.             (char *) NULL);
  792.         goto error;
  793.     }
  794.     } else if ((c == 's') && (length >= 2)
  795.         && (strncmp(argv[1], "size", length) == 0)) {
  796.     if (argc != 2) {
  797.         Tcl_AppendResult(interp, "wrong # args: should be \"",
  798.             argv[0], " size\"", (char *) NULL);
  799.         goto error;
  800.     }
  801.     sprintf(interp->result, "%d", listPtr->numElements);
  802.     } else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
  803.     int index, count, type, windowWidth, windowUnits;
  804.     int offset = 0;        /* Initialized to stop gcc warnings. */
  805.     double fraction, fraction2;
  806.  
  807.     windowWidth = Tk_Width(listPtr->tkwin)
  808.         - 2*(listPtr->inset + listPtr->selBorderWidth);
  809.     if (argc == 2) {
  810.         if (listPtr->maxWidth == 0) {
  811.         interp->result = "0 1";
  812.         } else {
  813.         fraction = listPtr->xOffset/((double) listPtr->maxWidth);
  814.         fraction2 = (listPtr->xOffset + windowWidth)
  815.             /((double) listPtr->maxWidth);
  816.         if (fraction2 > 1.0) {
  817.             fraction2 = 1.0;
  818.         }
  819.         sprintf(interp->result, "%g %g", fraction, fraction2);
  820.         }
  821.     } else if (argc == 3) {
  822.         if (Tcl_GetInt(interp, argv[2], &index) != TCL_OK) {
  823.         goto error;
  824.         }
  825.         ChangeListboxOffset(listPtr, index*listPtr->xScrollUnit);
  826.     } else {
  827.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  828.         switch (type) {
  829.         case TK_SCROLL_ERROR:
  830.             goto error;
  831.         case TK_SCROLL_MOVETO:
  832.             offset = fraction*listPtr->maxWidth + 0.5;
  833.             break;
  834.         case TK_SCROLL_PAGES:
  835.             windowUnits = windowWidth/listPtr->xScrollUnit;
  836.             if (windowUnits > 2) {
  837.             offset = listPtr->xOffset
  838.                 + count*listPtr->xScrollUnit*(windowUnits-2);
  839.             } else {
  840.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  841.             }
  842.             break;
  843.         case TK_SCROLL_UNITS:
  844.             offset = listPtr->xOffset + count*listPtr->xScrollUnit;
  845.             break;
  846.         }
  847.         ChangeListboxOffset(listPtr, offset);
  848.     }
  849.     } else if ((c == 'y') && (strncmp(argv[1], "yview", length) == 0)) {
  850.     int index, count, type;
  851.     double fraction, fraction2;
  852.  
  853.     if (argc == 2) {
  854.         if (listPtr->numElements == 0) {
  855.         interp->result = "0 1";
  856.         } else {
  857.         fraction = listPtr->topIndex/((double) listPtr->numElements);
  858.         fraction2 = (listPtr->topIndex+listPtr->fullLines)
  859.             /((double) listPtr->numElements);
  860.         if (fraction2 > 1.0) {
  861.             fraction2 = 1.0;
  862.         }
  863.         sprintf(interp->result, "%g %g", fraction, fraction2);
  864.         }
  865.     } else if (argc == 3) {
  866.         if (GetListboxIndex(interp, listPtr, argv[2], 0, &index)
  867.             != TCL_OK) {
  868.         goto error;
  869.         }
  870.         ChangeListboxView(listPtr, index);
  871.     } else {
  872.         type = Tk_GetScrollInfo(interp, argc, argv, &fraction, &count);
  873.         switch (type) {
  874.         case TK_SCROLL_ERROR:
  875.             goto error;
  876.         case TK_SCROLL_MOVETO:
  877.             index = listPtr->numElements*fraction + 0.5;
  878.             break;
  879.         case TK_SCROLL_PAGES:
  880.             if (listPtr->fullLines > 2) {
  881.             index = listPtr->topIndex
  882.                 + count*(listPtr->fullLines-2);
  883.             } else {
  884.             index = listPtr->topIndex + count;
  885.             }
  886.             break;
  887.         case TK_SCROLL_UNITS:
  888.             index = listPtr->topIndex + count;
  889.             break;
  890.         }
  891.         ChangeListboxView(listPtr, index);
  892.     }
  893.     } else {
  894.     Tcl_AppendResult(interp, "bad option \"", argv[1],
  895.         "\": must be activate, bbox, cget, configure, ",
  896.         "curselection, delete, get, index, insert, nearest, ",
  897.         "scan, see, selection, size, ",
  898.         "xview, or yview", (char *) NULL);
  899.     goto error;
  900.     }
  901.     Tcl_Release((ClientData) listPtr);
  902.     return result;
  903.  
  904.     error:
  905.     Tcl_Release((ClientData) listPtr);
  906.     return TCL_ERROR;
  907. }
  908.  
  909. /*
  910.  *----------------------------------------------------------------------
  911.  *
  912.  * DestroyListbox --
  913.  *
  914.  *    This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
  915.  *    to clean up the internal structure of a listbox at a safe time
  916.  *    (when no-one is using it anymore).
  917.  *
  918.  * Results:
  919.  *    None.
  920.  *
  921.  * Side effects:
  922.  *    Everything associated with the listbox is freed up.
  923.  *
  924.  *----------------------------------------------------------------------
  925.  */
  926.  
  927. static void
  928. DestroyListbox(memPtr)
  929.     char *memPtr;    /* Info about listbox widget. */
  930. {
  931.     register Listbox *listPtr = (Listbox *) memPtr;
  932.     register Element *elPtr, *nextPtr;
  933.  
  934.     /*
  935.      * Free up all of the list elements.
  936.      */
  937.  
  938.     for (elPtr = listPtr->firstPtr; elPtr != NULL; ) {
  939.     nextPtr = elPtr->nextPtr;
  940.     ckfree((char *) elPtr);
  941.     elPtr = nextPtr;
  942.     }
  943.  
  944.     /*
  945.      * Free up all the stuff that requires special handling, then
  946.      * let Tk_FreeOptions handle all the standard option-related
  947.      * stuff.
  948.      */
  949.  
  950.     if (listPtr->textGC != None) {
  951.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  952.     }
  953.     if (listPtr->selTextGC != None) {
  954.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  955.     }
  956.     Tk_FreeOptions(configSpecs, (char *) listPtr, listPtr->display, 0);
  957.     ckfree((char *) listPtr);
  958. }
  959.  
  960. /*
  961.  *----------------------------------------------------------------------
  962.  *
  963.  * ConfigureListbox --
  964.  *
  965.  *    This procedure is called to process an argv/argc list, plus
  966.  *    the Tk option database, in order to configure (or reconfigure)
  967.  *    a listbox widget.
  968.  *
  969.  * Results:
  970.  *    The return value is a standard Tcl result.  If TCL_ERROR is
  971.  *    returned, then interp->result contains an error message.
  972.  *
  973.  * Side effects:
  974.  *    Configuration information, such as colors, border width,
  975.  *    etc. get set for listPtr;  old resources get freed,
  976.  *    if there were any.
  977.  *
  978.  *----------------------------------------------------------------------
  979.  */
  980.  
  981. static int
  982. ConfigureListbox(interp, listPtr, argc, argv, flags)
  983.     Tcl_Interp *interp;        /* Used for error reporting. */
  984.     register Listbox *listPtr;    /* Information about widget;  may or may
  985.                  * not already have values for some fields. */
  986.     int argc;            /* Number of valid entries in argv. */
  987.     char **argv;        /* Arguments. */
  988.     int flags;            /* Flags to pass to Tk_ConfigureWidget. */
  989. {
  990.     XGCValues gcValues;
  991.     GC new;
  992.     int oldExport;
  993.  
  994.     oldExport = listPtr->exportSelection;
  995.     if (Tk_ConfigureWidget(interp, listPtr->tkwin, configSpecs,
  996.         argc, argv, (char *) listPtr, flags) != TCL_OK) {
  997.     return TCL_ERROR;
  998.     }
  999.  
  1000.     /*
  1001.      * A few options need special processing, such as setting the
  1002.      * background from a 3-D border.
  1003.      */
  1004.  
  1005.     Tk_SetBackgroundFromBorder(listPtr->tkwin, listPtr->normalBorder);
  1006.  
  1007.     if (listPtr->highlightWidth < 0) {
  1008.     listPtr->highlightWidth = 0;
  1009.     }
  1010.     listPtr->inset = listPtr->highlightWidth + listPtr->borderWidth;
  1011.  
  1012.     gcValues.foreground = listPtr->fgColorPtr->pixel;
  1013.     gcValues.font = listPtr->fontPtr->fid;
  1014.     gcValues.graphics_exposures = False;
  1015.     new = Tk_GetGC(listPtr->tkwin, GCForeground|GCFont|GCGraphicsExposures,
  1016.         &gcValues);
  1017.     if (listPtr->textGC != None) {
  1018.     Tk_FreeGC(listPtr->display, listPtr->textGC);
  1019.     }
  1020.     listPtr->textGC = new;
  1021.  
  1022.     gcValues.foreground = listPtr->selFgColorPtr->pixel;
  1023.     gcValues.font = listPtr->fontPtr->fid;
  1024.     new = Tk_GetGC(listPtr->tkwin, GCForeground|GCFont, &gcValues);
  1025.     if (listPtr->selTextGC != None) {
  1026.     Tk_FreeGC(listPtr->display, listPtr->selTextGC);
  1027.     }
  1028.     listPtr->selTextGC = new;
  1029.  
  1030.     /*
  1031.      * Claim the selection if we've suddenly started exporting it and
  1032.      * there is a selection to export.
  1033.      */
  1034.  
  1035.     if (listPtr->exportSelection && !oldExport
  1036.         && (listPtr->numSelected != 0)) {
  1037.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  1038.         (ClientData) listPtr);
  1039.     }
  1040.  
  1041.     /*
  1042.      * Register the desired geometry for the window and arrange for
  1043.      * the window to be redisplayed.
  1044.      */
  1045.  
  1046.     ListboxComputeGeometry(listPtr, 1, 1, 1);
  1047.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1048.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1049.     return TCL_OK;
  1050. }
  1051.  
  1052. /*
  1053.  *--------------------------------------------------------------
  1054.  *
  1055.  * DisplayListbox --
  1056.  *
  1057.  *    This procedure redraws the contents of a listbox window.
  1058.  *
  1059.  * Results:
  1060.  *    None.
  1061.  *
  1062.  * Side effects:
  1063.  *    Information appears on the screen.
  1064.  *
  1065.  *--------------------------------------------------------------
  1066.  */
  1067.  
  1068. static void
  1069. DisplayListbox(clientData)
  1070.     ClientData clientData;    /* Information about window. */
  1071. {
  1072.     register Listbox *listPtr = (Listbox *) clientData;
  1073.     register Tk_Window tkwin = listPtr->tkwin;
  1074.     register Element *elPtr;
  1075.     GC gc;
  1076.     int i, limit, x, y, width, prevSelected;
  1077.     int left, right;            /* Non-zero values here indicate
  1078.                      * that the left or right edge of
  1079.                      * the listbox is off-screen. */
  1080.     Pixmap pixmap;
  1081.  
  1082.     listPtr->flags &= ~REDRAW_PENDING;
  1083.     if (listPtr->flags & UPDATE_V_SCROLLBAR) {
  1084.     ListboxUpdateVScrollbar(listPtr);
  1085.     }
  1086.     if (listPtr->flags & UPDATE_H_SCROLLBAR) {
  1087.     ListboxUpdateHScrollbar(listPtr);
  1088.     }
  1089.     listPtr->flags &= ~(REDRAW_PENDING|UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR);
  1090.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(tkwin)) {
  1091.     return;
  1092.     }
  1093.  
  1094.     /*
  1095.      * Redrawing is done in a temporary pixmap that is allocated
  1096.      * here and freed at the end of the procedure.  All drawing is
  1097.      * done to the pixmap, and the pixmap is copied to the screen
  1098.      * at the end of the procedure.  This provides the smoothest
  1099.      * possible visual effects (no flashing on the screen).
  1100.      */
  1101.  
  1102.     pixmap = Tk_GetPixmap(listPtr->display, Tk_WindowId(tkwin),
  1103.         Tk_Width(tkwin), Tk_Height(tkwin), Tk_Depth(tkwin));
  1104.     Tk_Fill3DRectangle(tkwin, pixmap, listPtr->normalBorder, 0, 0,
  1105.         Tk_Width(tkwin), Tk_Height(tkwin), 0, TK_RELIEF_FLAT);
  1106.  
  1107.     /*
  1108.      * Iterate through all of the elements of the listbox, displaying each
  1109.      * in turn.  Selected elements use a different GC and have a raised
  1110.      * background.
  1111.      */
  1112.  
  1113.     limit = listPtr->topIndex + listPtr->fullLines + listPtr->partialLine - 1;
  1114.     if (limit >= listPtr->numElements) {
  1115.     limit = listPtr->numElements-1;
  1116.     }
  1117.     left = right = 0;
  1118.     if (listPtr->xOffset > 0) {
  1119.     left = listPtr->selBorderWidth+1;
  1120.     }
  1121.     if ((listPtr->maxWidth - listPtr->xOffset) > (Tk_Width(listPtr->tkwin)
  1122.         - 2*(listPtr->inset + listPtr->selBorderWidth)))  {
  1123.     right = listPtr->selBorderWidth+1;
  1124.     }
  1125.     prevSelected = 0;
  1126.     for (elPtr = listPtr->firstPtr, i = 0; (elPtr != NULL) && (i <= limit);
  1127.         prevSelected = elPtr->selected, elPtr = elPtr->nextPtr, i++) {
  1128.     if (i < listPtr->topIndex) {
  1129.         continue;
  1130.     }
  1131.     x = listPtr->inset;
  1132.     y = ((i - listPtr->topIndex) * listPtr->lineHeight) 
  1133.         + listPtr->inset;
  1134.     gc = listPtr->textGC;
  1135.     if (elPtr->selected) {
  1136.         gc = listPtr->selTextGC;
  1137.         width = Tk_Width(tkwin) - 2*listPtr->inset;
  1138.         Tk_Fill3DRectangle(tkwin, pixmap, listPtr->selBorder, x, y,
  1139.             width, listPtr->lineHeight, 0, TK_RELIEF_FLAT);
  1140.  
  1141.         /*
  1142.          * Draw beveled edges around the selection, if there are visible
  1143.          * edges next to this element.  Special considerations:
  1144.          * 1. The left and right bevels may not be visible if horizontal
  1145.          *    scrolling is enabled (the "left" and "right" variables
  1146.          *    are zero to indicate that the corresponding bevel is
  1147.          *    visible).
  1148.          * 2. Top and bottom bevels are only drawn if this is the
  1149.          *    first or last seleted item.
  1150.          * 3. If the left or right bevel isn't visible, then the "left"
  1151.          *    and "right" variables, computed above, have non-zero values
  1152.          *    that extend the top and bottom bevels so that the mitered
  1153.          *    corners are off-screen.
  1154.          */
  1155.  
  1156.         if (left == 0) {
  1157.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1158.             x, y, listPtr->selBorderWidth, listPtr->lineHeight,
  1159.             1, TK_RELIEF_RAISED);
  1160.         }
  1161.         if (right == 0) {
  1162.         Tk_3DVerticalBevel(tkwin, pixmap, listPtr->selBorder,
  1163.             x + width - listPtr->selBorderWidth, y,
  1164.             listPtr->selBorderWidth, listPtr->lineHeight,
  1165.             0, TK_RELIEF_RAISED);
  1166.         }
  1167.         if (!prevSelected) {
  1168.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder,
  1169.             x-left, y, width+left+right, listPtr->selBorderWidth,
  1170.             1, 1, 1, TK_RELIEF_RAISED);
  1171.         }
  1172.         if ((elPtr->nextPtr == NULL) || !elPtr->nextPtr->selected) {
  1173.         Tk_3DHorizontalBevel(tkwin, pixmap, listPtr->selBorder, x-left,
  1174.             y + listPtr->lineHeight - listPtr->selBorderWidth,
  1175.             width+left+right, listPtr->selBorderWidth, 0, 0, 0,
  1176.             TK_RELIEF_RAISED);
  1177.         }
  1178.     }
  1179.     y += listPtr->fontPtr->ascent + listPtr->selBorderWidth;
  1180.     x = listPtr->inset + listPtr->selBorderWidth - elPtr->lBearing
  1181.         - listPtr->xOffset;
  1182.     XDrawString(listPtr->display, pixmap, gc, x, y,
  1183.         elPtr->text, elPtr->textLength);
  1184.  
  1185.     /*
  1186.      * If this is the active element, underline it.
  1187.      */
  1188.  
  1189.     if ((i == listPtr->active) && (listPtr->flags & GOT_FOCUS)) {
  1190.         XFillRectangle(listPtr->display, pixmap, gc,
  1191.             listPtr->inset + listPtr->selBorderWidth
  1192.             - listPtr->xOffset,
  1193.             y + listPtr->fontPtr->descent - 1,
  1194.             (unsigned) elPtr->pixelWidth, 1);
  1195.     }
  1196.     }
  1197.  
  1198.     /*
  1199.      * Redraw the border for the listbox to make sure that it's on top
  1200.      * of any of the text of the listbox entries.
  1201.      */
  1202.  
  1203.     Tk_Draw3DRectangle(tkwin, pixmap, listPtr->normalBorder,
  1204.         listPtr->highlightWidth, listPtr->highlightWidth,
  1205.         Tk_Width(tkwin) - 2*listPtr->highlightWidth,
  1206.         Tk_Height(tkwin) - 2*listPtr->highlightWidth,
  1207.         listPtr->borderWidth, listPtr->relief);
  1208.     if (listPtr->highlightWidth > 0) {
  1209.     GC gc;
  1210.  
  1211.     if (listPtr->flags & GOT_FOCUS) {
  1212.         gc = Tk_GCForColor(listPtr->highlightColorPtr, pixmap);
  1213.     } else {
  1214.         gc = Tk_GCForColor(listPtr->highlightBgColorPtr, pixmap);
  1215.     }
  1216.     Tk_DrawFocusHighlight(tkwin, gc, listPtr->highlightWidth, pixmap);
  1217.     }
  1218.     XCopyArea(listPtr->display, pixmap, Tk_WindowId(tkwin),
  1219.         listPtr->textGC, 0, 0, (unsigned) Tk_Width(tkwin),
  1220.         (unsigned) Tk_Height(tkwin), 0, 0);
  1221.     Tk_FreePixmap(listPtr->display, pixmap);
  1222. }
  1223.  
  1224. /*
  1225.  *----------------------------------------------------------------------
  1226.  *
  1227.  * ListboxComputeGeometry --
  1228.  *
  1229.  *    This procedure is invoked to recompute geometry information
  1230.  *    such as the sizes of the elements and the overall dimensions
  1231.  *    desired for the listbox.
  1232.  *
  1233.  * Results:
  1234.  *    None.
  1235.  *
  1236.  * Side effects:
  1237.  *    Geometry information is updated and a new requested size is
  1238.  *    registered for the widget.  Internal border and gridding
  1239.  *    information is also set.
  1240.  *
  1241.  *----------------------------------------------------------------------
  1242.  */
  1243.  
  1244. static void
  1245. ListboxComputeGeometry(listPtr, fontChanged, maxIsStale, updateGrid)
  1246.     Listbox *listPtr;        /* Listbox whose geometry is to be
  1247.                  * recomputed. */
  1248.     int fontChanged;        /* Non-zero means the font may have changed
  1249.                  * so per-element width information also
  1250.                  * has to be computed. */
  1251.     int maxIsStale;        /* Non-zero means the "maxWidth" field may
  1252.                  * no longer be up-to-date and must
  1253.                  * be recomputed.  If fontChanged is 1 then
  1254.                  * this must be 1. */
  1255.     int updateGrid;        /* Non-zero means call Tk_SetGrid or
  1256.                  * Tk_UnsetGrid to update gridding for
  1257.                  * the window. */
  1258. {
  1259.     register Element *elPtr;
  1260.     int dummy, fontHeight, width, height, pixelWidth, pixelHeight;
  1261.     XCharStruct bbox;
  1262.  
  1263.     if (fontChanged  || maxIsStale) {
  1264.     listPtr->xScrollUnit = XTextWidth(listPtr->fontPtr, "0", 1);
  1265.     listPtr->maxWidth = 0;
  1266.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  1267.         if (fontChanged) {
  1268.         XTextExtents(listPtr->fontPtr, elPtr->text, elPtr->textLength,
  1269.             &dummy, &dummy, &dummy, &bbox);
  1270.         elPtr->lBearing = bbox.lbearing;
  1271.         elPtr->pixelWidth = bbox.rbearing - bbox.lbearing;
  1272.         }
  1273.         if (elPtr->pixelWidth > listPtr->maxWidth) {
  1274.         listPtr->maxWidth = elPtr->pixelWidth;
  1275.         }
  1276.     }
  1277.     }
  1278.  
  1279.     fontHeight = listPtr->fontPtr->ascent + listPtr->fontPtr->descent;
  1280.     listPtr->lineHeight = fontHeight + 1 + 2*listPtr->selBorderWidth;
  1281.     width = listPtr->width;
  1282.     if (width <= 0) {
  1283.     width = (listPtr->maxWidth + listPtr->xScrollUnit - 1)
  1284.         /listPtr->xScrollUnit;
  1285.     if (width < 1) {
  1286.         width = 1;
  1287.     }
  1288.     }
  1289.     pixelWidth = width*listPtr->xScrollUnit + 2*listPtr->inset
  1290.         + 2*listPtr->selBorderWidth;
  1291.     height = listPtr->height;
  1292.     if (listPtr->height <= 0) {
  1293.     height = listPtr->numElements;
  1294.     if (height < 1) {
  1295.         height = 1;
  1296.     }
  1297.     }
  1298.     pixelHeight = height*listPtr->lineHeight + 2*listPtr->inset;
  1299.     Tk_GeometryRequest(listPtr->tkwin, pixelWidth, pixelHeight);
  1300.     Tk_SetInternalBorder(listPtr->tkwin, listPtr->inset);
  1301.     if (updateGrid) {
  1302.     if (listPtr->setGrid) {
  1303.         Tk_SetGrid(listPtr->tkwin, width, height, listPtr->xScrollUnit,
  1304.             listPtr->lineHeight);
  1305.     } else {
  1306.         Tk_UnsetGrid(listPtr->tkwin);
  1307.     }
  1308.     }
  1309. }
  1310.  
  1311. /*
  1312.  *----------------------------------------------------------------------
  1313.  *
  1314.  * InsertEls --
  1315.  *
  1316.  *    Add new elements to a listbox widget.
  1317.  *
  1318.  * Results:
  1319.  *    None.
  1320.  *
  1321.  * Side effects:
  1322.  *    New information gets added to listPtr;  it will be redisplayed
  1323.  *    soon, but not immediately.
  1324.  *
  1325.  *----------------------------------------------------------------------
  1326.  */
  1327.  
  1328. static void
  1329. InsertEls(listPtr, index, argc, argv)
  1330.     register Listbox *listPtr;    /* Listbox that is to get the new
  1331.                  * elements. */
  1332.     int index;            /* Add the new elements before this
  1333.                  * element. */
  1334.     int argc;            /* Number of new elements to add. */
  1335.     char **argv;        /* New elements (one per entry). */
  1336. {
  1337.     register Element *prevPtr, *newPtr;
  1338.     int length, dummy, i, oldMaxWidth;
  1339.     XCharStruct bbox;
  1340.  
  1341.     /*
  1342.      * Find the element before which the new ones will be inserted.
  1343.      */
  1344.  
  1345.     if (index <= 0) {
  1346.     index = 0;
  1347.     }
  1348.     if (index > listPtr->numElements) {
  1349.     index = listPtr->numElements;
  1350.     }
  1351.     if (index == 0) {
  1352.     prevPtr = NULL;
  1353.     } else if (index == listPtr->numElements) {
  1354.           prevPtr = listPtr->lastPtr;
  1355.     } else {
  1356.     for (prevPtr = listPtr->firstPtr, i = index - 1; i > 0; i--) {
  1357.         prevPtr = prevPtr->nextPtr;
  1358.     }
  1359.     }
  1360.  
  1361.     /*
  1362.      * For each new element, create a record, initialize it, and link
  1363.      * it into the list of elements.
  1364.      */
  1365.  
  1366.     oldMaxWidth = listPtr->maxWidth;
  1367.     for (i = argc ; i > 0; i--, argv++, prevPtr = newPtr) {
  1368.     length = strlen(*argv);
  1369.     newPtr = (Element *) ckalloc(ElementSize(length));
  1370.     newPtr->textLength = length;
  1371.     strcpy(newPtr->text, *argv);
  1372.     XTextExtents(listPtr->fontPtr, newPtr->text, newPtr->textLength,
  1373.         &dummy, &dummy, &dummy, &bbox);
  1374.     newPtr->lBearing = bbox.lbearing;
  1375.     newPtr->pixelWidth = bbox.rbearing - bbox.lbearing;
  1376.     if (newPtr->pixelWidth > listPtr->maxWidth) {
  1377.         listPtr->maxWidth = newPtr->pixelWidth;
  1378.     }
  1379.     newPtr->selected = 0;
  1380.     if (prevPtr == NULL) {
  1381.         newPtr->nextPtr = listPtr->firstPtr;
  1382.         listPtr->firstPtr = newPtr;
  1383.     } else {
  1384.         newPtr->nextPtr = prevPtr->nextPtr;
  1385.         prevPtr->nextPtr = newPtr;
  1386.     }
  1387.     }
  1388.     if ((prevPtr != NULL) && (prevPtr->nextPtr == NULL)) {
  1389.     listPtr->lastPtr = prevPtr;
  1390.     }
  1391.     listPtr->numElements += argc;
  1392.  
  1393.     /*
  1394.      * Update the selection and other indexes to account for the
  1395.      * renumbering that has just occurred.  Then arrange for the new
  1396.      * information to be displayed.
  1397.      */
  1398.  
  1399.     if (index <= listPtr->selectAnchor) {
  1400.     listPtr->selectAnchor += argc;
  1401.     }
  1402.     if (index < listPtr->topIndex) {
  1403.     listPtr->topIndex += argc;
  1404.     }
  1405.     if (index <= listPtr->active) {
  1406.     listPtr->active += argc;
  1407.     if ((listPtr->active >= listPtr->numElements)
  1408.         && (listPtr->numElements > 0)) {
  1409.         listPtr->active = listPtr->numElements-1;
  1410.     }
  1411.     }
  1412.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1413.     if (listPtr->maxWidth != oldMaxWidth) {
  1414.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1415.     }
  1416.     ListboxComputeGeometry(listPtr, 0, 0, 0);
  1417.     ListboxRedrawRange(listPtr, index, listPtr->numElements-1);
  1418. }
  1419.  
  1420. /*
  1421.  *----------------------------------------------------------------------
  1422.  *
  1423.  * DeleteEls --
  1424.  *
  1425.  *    Remove one or more elements from a listbox widget.
  1426.  *
  1427.  * Results:
  1428.  *    None.
  1429.  *
  1430.  * Side effects:
  1431.  *    Memory gets freed, the listbox gets modified and (eventually)
  1432.  *    redisplayed.
  1433.  *
  1434.  *----------------------------------------------------------------------
  1435.  */
  1436.  
  1437. static void
  1438. DeleteEls(listPtr, first, last)
  1439.     register Listbox *listPtr;    /* Listbox widget to modify. */
  1440.     int first;            /* Index of first element to delete. */
  1441.     int last;            /* Index of last element to delete. */
  1442. {
  1443.     register Element *prevPtr, *elPtr;
  1444.     int count, i, widthChanged;
  1445.  
  1446.     /*
  1447.      * Adjust the range to fit within the existing elements of the
  1448.      * listbox, and make sure there's something to delete.
  1449.      */
  1450.  
  1451.     if (first < 0) {
  1452.     first = 0;
  1453.     }
  1454.     if (last >= listPtr->numElements) {
  1455.     last = listPtr->numElements-1;
  1456.     }
  1457.     count = last + 1 - first;
  1458.     if (count <= 0) {
  1459.     return;
  1460.     }
  1461.  
  1462.     /*
  1463.      * Find the element just before the ones to delete.
  1464.      */
  1465.  
  1466.     if (first == 0) {
  1467.     prevPtr = NULL;
  1468.     } else {
  1469.     for (i = first-1, prevPtr = listPtr->firstPtr; i > 0; i--) {
  1470.         prevPtr = prevPtr->nextPtr;
  1471.     }
  1472.     }
  1473.  
  1474.     /*
  1475.      * Delete the requested number of elements.
  1476.      */
  1477.  
  1478.     widthChanged = 0;
  1479.     for (i = count; i > 0; i--) {
  1480.     if (prevPtr == NULL) {
  1481.         elPtr = listPtr->firstPtr;
  1482.         listPtr->firstPtr = elPtr->nextPtr;
  1483.         if (listPtr->firstPtr == NULL) {
  1484.         listPtr->lastPtr = NULL;
  1485.         }
  1486.     } else {
  1487.         elPtr = prevPtr->nextPtr;
  1488.         prevPtr->nextPtr = elPtr->nextPtr;
  1489.         if (prevPtr->nextPtr == NULL) {
  1490.         listPtr->lastPtr = prevPtr;
  1491.         }
  1492.     }
  1493.     if (elPtr->pixelWidth == listPtr->maxWidth) {
  1494.         widthChanged = 1;
  1495.     }
  1496.     if (elPtr->selected) {
  1497.         listPtr->numSelected -= 1;
  1498.     }
  1499.     ckfree((char *) elPtr);
  1500.     }
  1501.     listPtr->numElements -= count;
  1502.  
  1503.     /*
  1504.      * Update the selection and viewing information to reflect the change
  1505.      * in the element numbering, and redisplay to slide information up over
  1506.      * the elements that were deleted.
  1507.      */
  1508.  
  1509.     if (first <= listPtr->selectAnchor) {
  1510.     listPtr->selectAnchor -= count;
  1511.     if (listPtr->selectAnchor < first) {
  1512.         listPtr->selectAnchor = first;
  1513.     }
  1514.     }
  1515.     if (first <= listPtr->topIndex) {
  1516.     listPtr->topIndex -= count;
  1517.     if (listPtr->topIndex < first) {
  1518.         listPtr->topIndex = first;
  1519.     }
  1520.     }
  1521.     if (listPtr->topIndex > (listPtr->numElements - listPtr->fullLines)) {
  1522.     listPtr->topIndex = listPtr->numElements - listPtr->fullLines;
  1523.     if (listPtr->topIndex < 0) {
  1524.         listPtr->topIndex = 0;
  1525.     }
  1526.     }
  1527.     if (listPtr->active > last) {
  1528.     listPtr->active -= count;
  1529.     } else if (listPtr->active >= first) {
  1530.     listPtr->active = first;
  1531.     if ((listPtr->active >= listPtr->numElements)
  1532.         && (listPtr->numElements > 0)) {
  1533.         listPtr->active = listPtr->numElements-1;
  1534.     }
  1535.     }
  1536.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1537.     ListboxComputeGeometry(listPtr, 0, widthChanged, 0);
  1538.     if (widthChanged) {
  1539.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1540.     }
  1541.     ListboxRedrawRange(listPtr, first, listPtr->numElements-1);
  1542. }
  1543.  
  1544. /*
  1545.  *--------------------------------------------------------------
  1546.  *
  1547.  * ListboxEventProc --
  1548.  *
  1549.  *    This procedure is invoked by the Tk dispatcher for various
  1550.  *    events on listboxes.
  1551.  *
  1552.  * Results:
  1553.  *    None.
  1554.  *
  1555.  * Side effects:
  1556.  *    When the window gets deleted, internal structures get
  1557.  *    cleaned up.  When it gets exposed, it is redisplayed.
  1558.  *
  1559.  *--------------------------------------------------------------
  1560.  */
  1561.  
  1562. static void
  1563. ListboxEventProc(clientData, eventPtr)
  1564.     ClientData clientData;    /* Information about window. */
  1565.     XEvent *eventPtr;        /* Information about event. */
  1566. {
  1567.     Listbox *listPtr = (Listbox *) clientData;
  1568.  
  1569.     if (eventPtr->type == Expose) {
  1570.     ListboxRedrawRange(listPtr,
  1571.         NearestListboxElement(listPtr, eventPtr->xexpose.y),
  1572.         NearestListboxElement(listPtr, eventPtr->xexpose.y
  1573.         + eventPtr->xexpose.height));
  1574.     } else if (eventPtr->type == DestroyNotify) {
  1575.     if (listPtr->setGrid) {
  1576.         Tk_UnsetGrid(listPtr->tkwin);
  1577.     }
  1578.     if (listPtr->tkwin != NULL) {
  1579.         listPtr->tkwin = NULL;
  1580.         Tcl_DeleteCommand(listPtr->interp,
  1581.             Tcl_GetCommandName(listPtr->interp, listPtr->widgetCmd));
  1582.     }
  1583.     if (listPtr->flags & REDRAW_PENDING) {
  1584.         Tcl_CancelIdleCall(DisplayListbox, (ClientData) listPtr);
  1585.     }
  1586.     Tcl_EventuallyFree((ClientData) listPtr, DestroyListbox);
  1587.     } else if (eventPtr->type == ConfigureNotify) {
  1588.     int vertSpace;
  1589.  
  1590.     vertSpace = Tk_Height(listPtr->tkwin) - 2*listPtr->inset;
  1591.     listPtr->fullLines = vertSpace / listPtr->lineHeight;
  1592.     if ((listPtr->fullLines*listPtr->lineHeight) < vertSpace) {
  1593.         listPtr->partialLine = 1;
  1594.     } else {
  1595.         listPtr->partialLine = 0;
  1596.     }
  1597.     listPtr->flags |= UPDATE_V_SCROLLBAR|UPDATE_H_SCROLLBAR;
  1598.     ChangeListboxView(listPtr, listPtr->topIndex);
  1599.     ChangeListboxOffset(listPtr, listPtr->xOffset);
  1600.  
  1601.     /*
  1602.      * Redraw the whole listbox.  It's hard to tell what needs
  1603.      * to be redrawn (e.g. if the listbox has shrunk then we
  1604.      * may only need to redraw the borders), so just redraw
  1605.      * everything for safety.
  1606.      */
  1607.  
  1608.     ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1609.     } else if (eventPtr->type == FocusIn) {
  1610.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1611.         listPtr->flags |= GOT_FOCUS;
  1612.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1613.     }
  1614.     } else if (eventPtr->type == FocusOut) {
  1615.     if (eventPtr->xfocus.detail != NotifyInferior) {
  1616.         listPtr->flags &= ~GOT_FOCUS;
  1617.         ListboxRedrawRange(listPtr, 0, listPtr->numElements-1);
  1618.     }
  1619.     }
  1620. }
  1621.  
  1622. /*
  1623.  *----------------------------------------------------------------------
  1624.  *
  1625.  * ListboxCmdDeletedProc --
  1626.  *
  1627.  *    This procedure is invoked when a widget command is deleted.  If
  1628.  *    the widget isn't already in the process of being destroyed,
  1629.  *    this command destroys it.
  1630.  *
  1631.  * Results:
  1632.  *    None.
  1633.  *
  1634.  * Side effects:
  1635.  *    The widget is destroyed.
  1636.  *
  1637.  *----------------------------------------------------------------------
  1638.  */
  1639.  
  1640. static void
  1641. ListboxCmdDeletedProc(clientData)
  1642.     ClientData clientData;    /* Pointer to widget record for widget. */
  1643. {
  1644.     Listbox *listPtr = (Listbox *) clientData;
  1645.     Tk_Window tkwin = listPtr->tkwin;
  1646.  
  1647.     /*
  1648.      * This procedure could be invoked either because the window was
  1649.      * destroyed and the command was then deleted (in which case tkwin
  1650.      * is NULL) or because the command was deleted, and then this procedure
  1651.      * destroys the widget.
  1652.      */
  1653.  
  1654.     if (tkwin != NULL) {
  1655.     listPtr->tkwin = NULL;
  1656.     Tk_DestroyWindow(tkwin);
  1657.     }
  1658. }
  1659.  
  1660. /*
  1661.  *--------------------------------------------------------------
  1662.  *
  1663.  * GetListboxIndex --
  1664.  *
  1665.  *    Parse an index into a listbox and return either its value
  1666.  *    or an error.
  1667.  *
  1668.  * Results:
  1669.  *    A standard Tcl result.  If all went well, then *indexPtr is
  1670.  *    filled in with the index (into listPtr) corresponding to
  1671.  *    string.  Otherwise an error message is left in interp->result.
  1672.  *
  1673.  * Side effects:
  1674.  *    None.
  1675.  *
  1676.  *--------------------------------------------------------------
  1677.  */
  1678.  
  1679. static int
  1680. GetListboxIndex(interp, listPtr, string, numElsOK, indexPtr)
  1681.     Tcl_Interp *interp;        /* For error messages. */
  1682.     Listbox *listPtr;        /* Listbox for which the index is being
  1683.                  * specified. */
  1684.     char *string;        /* Specifies an element in the listbox. */
  1685.     int numElsOK;        /* 0 means the return value must be less
  1686.                  * less than the number of entries in
  1687.                  * the listbox;  1 means it may also be
  1688.                  * equal to the number of entries. */
  1689.     int *indexPtr;        /* Where to store converted index. */
  1690. {
  1691.     int c;
  1692.     size_t length;
  1693.  
  1694.     length = strlen(string);
  1695.     c = string[0];
  1696.     if ((c == 'a') && (strncmp(string, "active", length) == 0)
  1697.         && (length >= 2)) {
  1698.     *indexPtr = listPtr->active;
  1699.     } else if ((c == 'a') && (strncmp(string, "anchor", length) == 0)
  1700.         && (length >= 2)) {
  1701.     *indexPtr = listPtr->selectAnchor;
  1702.     } else if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
  1703.     *indexPtr = listPtr->numElements;
  1704.     } else if (c == '@') {
  1705.     int x, y;
  1706.     char *p, *end;
  1707.  
  1708.     p = string+1;
  1709.     x = strtol(p, &end, 0);
  1710.     if ((end == p) || (*end != ',')) {
  1711.         goto badIndex;
  1712.     }
  1713.     p = end+1;
  1714.     y = strtol(p, &end, 0);
  1715.     if ((end == p) || (*end != 0)) {
  1716.         goto badIndex;
  1717.     }
  1718.     *indexPtr = NearestListboxElement(listPtr, y);
  1719.     } else {
  1720.     if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
  1721.         Tcl_ResetResult(interp);
  1722.         goto badIndex;
  1723.     }
  1724.     }
  1725.     if (numElsOK) {
  1726.     if (*indexPtr > listPtr->numElements) {
  1727.         *indexPtr = listPtr->numElements;
  1728.     }
  1729.     } else if (*indexPtr >= listPtr->numElements) {
  1730.     *indexPtr = listPtr->numElements-1;
  1731.     }
  1732.     if (*indexPtr < 0) {
  1733.     *indexPtr = 0;
  1734.     }
  1735.     return TCL_OK;
  1736.  
  1737.     badIndex:
  1738.     Tcl_AppendResult(interp, "bad listbox index \"", string,
  1739.         "\": must be active, anchor, end, @x,y, or a number",
  1740.         (char *) NULL);
  1741.     return TCL_ERROR;
  1742. }
  1743.  
  1744. /*
  1745.  *----------------------------------------------------------------------
  1746.  *
  1747.  * ChangeListboxView --
  1748.  *
  1749.  *    Change the view on a listbox widget so that a given element
  1750.  *    is displayed at the top.
  1751.  *
  1752.  * Results:
  1753.  *    None.
  1754.  *
  1755.  * Side effects:
  1756.  *    What's displayed on the screen is changed.  If there is a
  1757.  *    scrollbar associated with this widget, then the scrollbar
  1758.  *    is instructed to change its display too.
  1759.  *
  1760.  *----------------------------------------------------------------------
  1761.  */
  1762.  
  1763. static void
  1764. ChangeListboxView(listPtr, index)
  1765.     register Listbox *listPtr;        /* Information about widget. */
  1766.     int index;                /* Index of element in listPtr
  1767.                      * that should now appear at the
  1768.                      * top of the listbox. */
  1769. {
  1770.     if (index >= (listPtr->numElements - listPtr->fullLines)) {
  1771.     index = listPtr->numElements - listPtr->fullLines;
  1772.     }
  1773.     if (index < 0) {
  1774.     index = 0;
  1775.     }
  1776.     if (listPtr->topIndex != index) {
  1777.     listPtr->topIndex = index;
  1778.     if (!(listPtr->flags & REDRAW_PENDING)) {
  1779.         Tcl_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  1780.         listPtr->flags |= REDRAW_PENDING;
  1781.     }
  1782.     listPtr->flags |= UPDATE_V_SCROLLBAR;
  1783.     }
  1784. }
  1785.  
  1786. /*
  1787.  *----------------------------------------------------------------------
  1788.  *
  1789.  * ChangListboxOffset --
  1790.  *
  1791.  *    Change the horizontal offset for a listbox.
  1792.  *
  1793.  * Results:
  1794.  *    None.
  1795.  *
  1796.  * Side effects:
  1797.  *    The listbox may be redrawn to reflect its new horizontal
  1798.  *    offset.
  1799.  *
  1800.  *----------------------------------------------------------------------
  1801.  */
  1802.  
  1803. static void
  1804. ChangeListboxOffset(listPtr, offset)
  1805.     register Listbox *listPtr;        /* Information about widget. */
  1806.     int offset;                /* Desired new "xOffset" for
  1807.                      * listbox. */
  1808. {
  1809.     int maxOffset;
  1810.  
  1811.     /*
  1812.      * Make sure that the new offset is within the allowable range, and
  1813.      * round it off to an even multiple of xScrollUnit.
  1814.      */
  1815.  
  1816.     maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit-1)
  1817.         - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
  1818.         - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
  1819.     if (offset > maxOffset) {
  1820.     offset = maxOffset;
  1821.     }
  1822.     if (offset < 0) {
  1823.     offset = 0;
  1824.     }
  1825.     offset -= offset%listPtr->xScrollUnit;
  1826.     if (offset != listPtr->xOffset) {
  1827.     listPtr->xOffset = offset;
  1828.     listPtr->flags |= UPDATE_H_SCROLLBAR;
  1829.     ListboxRedrawRange(listPtr, 0, listPtr->numElements);
  1830.     }
  1831. }
  1832.  
  1833. /*
  1834.  *----------------------------------------------------------------------
  1835.  *
  1836.  * ListboxScanTo --
  1837.  *
  1838.  *    Given a point (presumably of the curent mouse location)
  1839.  *    drag the view in the window to implement the scan operation.
  1840.  *
  1841.  * Results:
  1842.  *    None.
  1843.  *
  1844.  * Side effects:
  1845.  *    The view in the window may change.
  1846.  *
  1847.  *----------------------------------------------------------------------
  1848.  */
  1849.  
  1850. static void
  1851. ListboxScanTo(listPtr, x, y)
  1852.     register Listbox *listPtr;        /* Information about widget. */
  1853.     int x;                /* X-coordinate to use for scan
  1854.                      * operation. */
  1855.     int y;                /* Y-coordinate to use for scan
  1856.                      * operation. */
  1857. {
  1858.     int newTopIndex, newOffset, maxIndex, maxOffset;
  1859.  
  1860.     maxIndex = listPtr->numElements - listPtr->fullLines;
  1861.     maxOffset = listPtr->maxWidth + (listPtr->xScrollUnit-1)
  1862.         - (Tk_Width(listPtr->tkwin) - 2*listPtr->inset
  1863.         - 2*listPtr->selBorderWidth - listPtr->xScrollUnit);
  1864.  
  1865.     /*
  1866.      * Compute new top line for screen by amplifying the difference
  1867.      * between the current position and the place where the scan
  1868.      * started (the "mark" position).  If we run off the top or bottom
  1869.      * of the list, then reset the mark point so that the current
  1870.      * position continues to correspond to the edge of the window.
  1871.      * This means that the picture will start dragging as soon as the
  1872.      * mouse reverses direction (without this reset, might have to slide
  1873.      * mouse a long ways back before the picture starts moving again).
  1874.      */
  1875.  
  1876.     newTopIndex = listPtr->scanMarkYIndex
  1877.         - (10*(y - listPtr->scanMarkY))/listPtr->lineHeight;
  1878.     if (newTopIndex > maxIndex) {
  1879.     newTopIndex = listPtr->scanMarkYIndex = maxIndex;
  1880.     listPtr->scanMarkY = y;
  1881.     } else if (newTopIndex < 0) {
  1882.     newTopIndex = listPtr->scanMarkYIndex = 0;
  1883.     listPtr->scanMarkY = y;
  1884.     }
  1885.     ChangeListboxView(listPtr, newTopIndex);
  1886.  
  1887.     /*
  1888.      * Compute new left edge for display in a similar fashion by amplifying
  1889.      * the difference between the current position and the place where the
  1890.      * scan started.
  1891.      */
  1892.  
  1893.     newOffset = listPtr->scanMarkXOffset - (10*(x - listPtr->scanMarkX));
  1894.     if (newOffset > maxOffset) {
  1895.     newOffset = listPtr->scanMarkXOffset = maxOffset;
  1896.     listPtr->scanMarkX = x;
  1897.     } else if (newOffset < 0) {
  1898.     newOffset = listPtr->scanMarkXOffset = 0;
  1899.     listPtr->scanMarkX = x;
  1900.     }
  1901.     ChangeListboxOffset(listPtr, newOffset);
  1902. }
  1903.  
  1904. /*
  1905.  *----------------------------------------------------------------------
  1906.  *
  1907.  * NearestListboxElement --
  1908.  *
  1909.  *    Given a y-coordinate inside a listbox, compute the index of
  1910.  *    the element under that y-coordinate (or closest to that
  1911.  *    y-coordinate).
  1912.  *
  1913.  * Results:
  1914.  *    The return value is an index of an element of listPtr.  If
  1915.  *    listPtr has no elements, then 0 is always returned.
  1916.  *
  1917.  * Side effects:
  1918.  *    None.
  1919.  *
  1920.  *----------------------------------------------------------------------
  1921.  */
  1922.  
  1923. static int
  1924. NearestListboxElement(listPtr, y)
  1925.     register Listbox *listPtr;        /* Information about widget. */
  1926.     int y;                /* Y-coordinate in listPtr's window. */
  1927. {
  1928.     int index;
  1929.  
  1930.     index = (y - listPtr->inset)/listPtr->lineHeight;
  1931.     if (index >= (listPtr->fullLines + listPtr->partialLine)) {
  1932.     index = listPtr->fullLines + listPtr->partialLine - 1;
  1933.     }
  1934.     if (index < 0) {
  1935.     index = 0;
  1936.     }
  1937.     index += listPtr->topIndex;
  1938.     if (index >= listPtr->numElements) {
  1939.     index = listPtr->numElements-1;
  1940.     }
  1941.     return index;
  1942. }
  1943.  
  1944. /*
  1945.  *----------------------------------------------------------------------
  1946.  *
  1947.  * ListboxSelect --
  1948.  *
  1949.  *    Select or deselect one or more elements in a listbox..
  1950.  *
  1951.  * Results:
  1952.  *    None.
  1953.  *
  1954.  * Side effects:
  1955.  *    All of the elements in the range between first and last are
  1956.  *    marked as either selected or deselected, depending on the
  1957.  *    "select" argument.  Any items whose state changes are redisplayed.
  1958.  *    The selection is claimed from X when the number of selected
  1959.  *    elements changes from zero to non-zero.
  1960.  *
  1961.  *----------------------------------------------------------------------
  1962.  */
  1963.  
  1964. static void
  1965. ListboxSelect(listPtr, first, last, select)
  1966.     register Listbox *listPtr;        /* Information about widget. */
  1967.     int first;                /* Index of first element to
  1968.                      * select or deselect. */
  1969.     int last;                /* Index of last element to
  1970.                      * select or deselect. */
  1971.     int select;                /* 1 means select items, 0 means
  1972.                      * deselect them. */
  1973. {
  1974.     int i, firstRedisplay, lastRedisplay, increment, oldCount;
  1975.     Element *elPtr;
  1976.  
  1977.     if (last < first) {
  1978.     i = first;
  1979.     first = last;
  1980.     last = i;
  1981.     }
  1982.     if (first >= listPtr->numElements) {
  1983.     return;
  1984.     }
  1985.     oldCount = listPtr->numSelected;
  1986.     firstRedisplay = -1;
  1987.     increment = select ? 1 : -1;
  1988.     for (i = 0, elPtr = listPtr->firstPtr; i < first;
  1989.         i++, elPtr = elPtr->nextPtr) {
  1990.     /* Empty loop body. */
  1991.     }
  1992.     for ( ; i <= last; i++, elPtr = elPtr->nextPtr) {
  1993.     if (elPtr->selected == select) {
  1994.         continue;
  1995.     }
  1996.     listPtr->numSelected += increment;
  1997.     elPtr->selected = select;
  1998.     if (firstRedisplay < 0) {
  1999.         firstRedisplay = i;
  2000.     }
  2001.     lastRedisplay = i;
  2002.     }
  2003.     if (firstRedisplay >= 0) {
  2004.     ListboxRedrawRange(listPtr, first, last);
  2005.     }
  2006.     if ((oldCount == 0) && (listPtr->numSelected > 0)
  2007.         && (listPtr->exportSelection)) {
  2008.     Tk_OwnSelection(listPtr->tkwin, XA_PRIMARY, ListboxLostSelection,
  2009.         (ClientData) listPtr);
  2010.     }
  2011. }
  2012.  
  2013. /*
  2014.  *----------------------------------------------------------------------
  2015.  *
  2016.  * ListboxFetchSelection --
  2017.  *
  2018.  *    This procedure is called back by Tk when the selection is
  2019.  *    requested by someone.  It returns part or all of the selection
  2020.  *    in a buffer provided by the caller.
  2021.  *
  2022.  * Results:
  2023.  *    The return value is the number of non-NULL bytes stored
  2024.  *    at buffer.  Buffer is filled (or partially filled) with a
  2025.  *    NULL-terminated string containing part or all of the selection,
  2026.  *    as given by offset and maxBytes.  The selection is returned
  2027.  *    as a Tcl list with one list element for each element in the
  2028.  *    listbox.
  2029.  *
  2030.  * Side effects:
  2031.  *    None.
  2032.  *
  2033.  *----------------------------------------------------------------------
  2034.  */
  2035.  
  2036. static int
  2037. ListboxFetchSelection(clientData, offset, buffer, maxBytes)
  2038.     ClientData clientData;        /* Information about listbox widget. */
  2039.     int offset;                /* Offset within selection of first
  2040.                      * byte to be returned. */
  2041.     char *buffer;            /* Location in which to place
  2042.                      * selection. */
  2043.     int maxBytes;            /* Maximum number of bytes to place
  2044.                      * at buffer, not including terminating
  2045.                      * NULL character. */
  2046. {
  2047.     register Listbox *listPtr = (Listbox *) clientData;
  2048.     register Element *elPtr;
  2049.     Tcl_DString selection;
  2050.     int length, count, needNewline;
  2051.  
  2052.     if (!listPtr->exportSelection) {
  2053.     return -1;
  2054.     }
  2055.  
  2056.     /*
  2057.      * Use a dynamic string to accumulate the contents of the selection.
  2058.      */
  2059.  
  2060.     needNewline = 0;
  2061.     Tcl_DStringInit(&selection);
  2062.     for (elPtr = listPtr->firstPtr; elPtr != NULL; elPtr = elPtr->nextPtr) {
  2063.     if (elPtr->selected) {
  2064.         if (needNewline) {
  2065.         Tcl_DStringAppend(&selection, "\n", 1);
  2066.         }
  2067.         Tcl_DStringAppend(&selection, elPtr->text, elPtr->textLength);
  2068.         needNewline = 1;
  2069.     }
  2070.     }
  2071.  
  2072.     length = Tcl_DStringLength(&selection);
  2073.     if (length == 0) {
  2074.     return -1;
  2075.     }
  2076.  
  2077.     /*
  2078.      * Copy the requested portion of the selection to the buffer.
  2079.      */
  2080.  
  2081.     count = length - offset;
  2082.     if (count <= 0) {
  2083.     count = 0;
  2084.     } else {
  2085.     if (count > maxBytes) {
  2086.         count = maxBytes;
  2087.     }
  2088.     memcpy((VOID *) buffer,
  2089.         (VOID *) (Tcl_DStringValue(&selection) + offset),
  2090.         (size_t) count);
  2091.     }
  2092.     buffer[count] = '\0';
  2093.     Tcl_DStringFree(&selection);
  2094.     return count;
  2095. }
  2096.  
  2097. /*
  2098.  *----------------------------------------------------------------------
  2099.  *
  2100.  * ListboxLostSelection --
  2101.  *
  2102.  *    This procedure is called back by Tk when the selection is
  2103.  *    grabbed away from a listbox widget.
  2104.  *
  2105.  * Results:
  2106.  *    None.
  2107.  *
  2108.  * Side effects:
  2109.  *    The existing selection is unhighlighted, and the window is
  2110.  *    marked as not containing a selection.
  2111.  *
  2112.  *----------------------------------------------------------------------
  2113.  */
  2114.  
  2115. static void
  2116. ListboxLostSelection(clientData)
  2117.     ClientData clientData;        /* Information about listbox widget. */
  2118. {
  2119.     register Listbox *listPtr = (Listbox *) clientData;
  2120.  
  2121.     if ((listPtr->exportSelection) && (listPtr->numElements > 0)) {
  2122.     ListboxSelect(listPtr, 0, listPtr->numElements-1, 0);
  2123.     }
  2124. }
  2125.  
  2126. /*
  2127.  *----------------------------------------------------------------------
  2128.  *
  2129.  * ListboxRedrawRange --
  2130.  *
  2131.  *    Ensure that a given range of elements is eventually redrawn on
  2132.  *    the display (if those elements in fact appear on the display).
  2133.  *
  2134.  * Results:
  2135.  *    None.
  2136.  *
  2137.  * Side effects:
  2138.  *    Information gets redisplayed.
  2139.  *
  2140.  *----------------------------------------------------------------------
  2141.  */
  2142.  
  2143.     /* ARGSUSED */
  2144. static void
  2145. ListboxRedrawRange(listPtr, first, last)
  2146.     register Listbox *listPtr;        /* Information about widget. */
  2147.     int first;                /* Index of first element in list
  2148.                      * that needs to be redrawn. */
  2149.     int last;                /* Index of last element in list
  2150.                      * that needs to be redrawn.  May
  2151.                      * be less than first;
  2152.                      * these just bracket a range. */
  2153. {
  2154.     if ((listPtr->tkwin == NULL) || !Tk_IsMapped(listPtr->tkwin)
  2155.         || (listPtr->flags & REDRAW_PENDING)) {
  2156.     return;
  2157.     }
  2158.     Tcl_DoWhenIdle(DisplayListbox, (ClientData) listPtr);
  2159.     listPtr->flags |= REDRAW_PENDING;
  2160. }
  2161.  
  2162. /*
  2163.  *----------------------------------------------------------------------
  2164.  *
  2165.  * ListboxUpdateVScrollbar --
  2166.  *
  2167.  *    This procedure is invoked whenever information has changed in
  2168.  *    a listbox in a way that would invalidate a vertical scrollbar
  2169.  *    display.  If there is an associated scrollbar, then this command
  2170.  *    updates it by invoking a Tcl command.
  2171.  *
  2172.  * Results:
  2173.  *    None.
  2174.  *
  2175.  * Side effects:
  2176.  *    A Tcl command is invoked, and an additional command may be
  2177.  *    invoked to process errors in the command.
  2178.  *
  2179.  *----------------------------------------------------------------------
  2180.  */
  2181.  
  2182. static void
  2183. ListboxUpdateVScrollbar(listPtr)
  2184.     register Listbox *listPtr;        /* Information about widget. */
  2185. {
  2186.     char string[100];
  2187.     double first, last;
  2188.     int result;
  2189.     Tcl_Interp *interp;
  2190.  
  2191.     if (listPtr->yScrollCmd == NULL) {
  2192.     return;
  2193.     }
  2194.     if (listPtr->numElements == 0) {
  2195.     first = 0.0;
  2196.     last = 1.0;
  2197.     } else {
  2198.     first = listPtr->topIndex/((double) listPtr->numElements);
  2199.     last = (listPtr->topIndex+listPtr->fullLines)
  2200.         /((double) listPtr->numElements);
  2201.     if (last > 1.0) {
  2202.         last = 1.0;
  2203.     }
  2204.     }
  2205.     sprintf(string, " %g %g", first, last);
  2206.  
  2207.     /*
  2208.      * We must hold onto the interpreter from the listPtr because the data
  2209.      * at listPtr might be freed as a result of the Tcl_VarEval.
  2210.      */
  2211.     
  2212.     interp = listPtr->interp;
  2213.     Tcl_Preserve((ClientData) interp);
  2214.     result = Tcl_VarEval(interp, listPtr->yScrollCmd, string,
  2215.         (char *) NULL);
  2216.     if (result != TCL_OK) {
  2217.     Tcl_AddErrorInfo(interp,
  2218.         "\n    (vertical scrolling command executed by listbox)");
  2219.     Tcl_BackgroundError(interp);
  2220.     }
  2221.     Tcl_Release((ClientData) interp);
  2222. }
  2223.  
  2224. /*
  2225.  *----------------------------------------------------------------------
  2226.  *
  2227.  * ListboxUpdateHScrollbar --
  2228.  *
  2229.  *    This procedure is invoked whenever information has changed in
  2230.  *    a listbox in a way that would invalidate a horizontal scrollbar
  2231.  *    display.  If there is an associated horizontal scrollbar, then
  2232.  *    this command updates it by invoking a Tcl command.
  2233.  *
  2234.  * Results:
  2235.  *    None.
  2236.  *
  2237.  * Side effects:
  2238.  *    A Tcl command is invoked, and an additional command may be
  2239.  *    invoked to process errors in the command.
  2240.  *
  2241.  *----------------------------------------------------------------------
  2242.  */
  2243.  
  2244. static void
  2245. ListboxUpdateHScrollbar(listPtr)
  2246.     register Listbox *listPtr;        /* Information about widget. */
  2247. {
  2248.     char string[60];
  2249.     int result, windowWidth;
  2250.     double first, last;
  2251.     Tcl_Interp *interp;
  2252.  
  2253.     if (listPtr->xScrollCmd == NULL) {
  2254.     return;
  2255.     }
  2256.     windowWidth = Tk_Width(listPtr->tkwin) - 2*(listPtr->inset
  2257.         + listPtr->selBorderWidth);
  2258.     if (listPtr->maxWidth == 0) {
  2259.     first = 0;
  2260.     last = 1.0;
  2261.     } else {
  2262.     first = listPtr->xOffset/((double) listPtr->maxWidth);
  2263.     last = (listPtr->xOffset + windowWidth)
  2264.         /((double) listPtr->maxWidth);
  2265.     if (last > 1.0) {
  2266.         last = 1.0;
  2267.     }
  2268.     }
  2269.     sprintf(string, " %g %g", first, last);
  2270.  
  2271.     /*
  2272.      * We must hold onto the interpreter because the data referred to at
  2273.      * listPtr might be freed as a result of the call to Tcl_VarEval.
  2274.      */
  2275.     
  2276.     interp = listPtr->interp;
  2277.     Tcl_Preserve((ClientData) interp);
  2278.     result = Tcl_VarEval(interp, listPtr->xScrollCmd, string,
  2279.         (char *) NULL);
  2280.     if (result != TCL_OK) {
  2281.     Tcl_AddErrorInfo(interp,
  2282.         "\n    (horizontal scrolling command executed by listbox)");
  2283.     Tcl_BackgroundError(interp);
  2284.     }
  2285.     Tcl_Release((ClientData) interp);
  2286. }
  2287.